/**
 *  Version:     @(#)aio_log.h    0.2.11 20/03/2007
 *
 *  Authors:     Hailong Xia <hlxxxx@gmail.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty
 *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *  the GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *  Boston, MA 02111-1307  USA
 */

#ifndef __AIO_LOG_H
#define __AIO_LOG_H

/**
 * This log don't support threadsafe unless you write syslog. So it only 
 * was used at AIO library debug phase or single-threads environment. 
 * If you want to use libaio under multi-threads environment, you should 
 * close AIO log function by define macro _AIO_NOLOG.
 */

#if defined(__cplusplus)
extern "C" {
#endif

#define AIO_LOG_BUFSIZE 2048

int get_syslog_level();
void log_set_silence_level(int level);
void log_set_tosyslog(int tosyslog);
void log_set_syslog_level(const char *level);
void log_set_output_file(const char *filedir);

/**
 * What shall I log to syslog?
 * 0 - DEBUGs, INFOs, WARNINGs and ERRORs (this is the default)
 * 1 - INFOs, WARNINGs and ERRORs
 * 2 - WARNINGs and ERRORs
 * 3 - only ERRORs
 * 4 - absolutely nothing (be careful - you will have no way to
 */

void log_f(const char *subsys, 
        const char *file, int line, const char *format, ...);

#ifndef _AIO_NOLOG

/**
 * Variadic Macros notice:
 * Only support for variadic macros was introduced in Visual C++ 2005.
 * Below usage only support by C99, don't compile as a c++ file.
 * There is a expand usage in GCC, it may be complie success both c and c++.
 * eg: log_err(F...) error_f(__FILE__, __LINE__, F)
 */

extern log_silence_level; /* global variant */

#define debugp(...)                                               \
do {                                                              \
        if (log_silence_level < 1)                                \
                log_f("DEBUG", __FILE__, __LINE__, __VA_ARGS__);  \
} while (0)

#define log_info(...)                                             \
do {                                                              \
        if (log_silence_level < 2)                                \
                log_f("INFO", __FILE__, __LINE__, __VA_ARGS__);   \
} while (0)

#define log_warn(...)                                             \
do {                                                              \
        if (log_silence_level < 3)                                \
                log_f("WARNING", __FILE__, __LINE__, __VA_ARGS__);\
} while (0)

#define log_err(...) log_f("ERROR", __FILE__, __LINE__, __VA_ARGS__)

#else /* The case for don't support variadic macro compiler */

#define debugp        printf
#define log_err       printf
#define log_warn      printf
#define log_info      printf

#endif /* ifndef _AIO_NOLOG */

#if defined(__cplusplus)
}
#endif

#endif  /* __AIO_LOG_H */

